package com.hc.controller;

import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.constant.GlobalConstant;
import com.hc.domain.ProductSpecs;
import com.hc.query.ProductCategoryCondition;
import com.hc.query.ProductSpecsCondition;
import com.hc.res.IndexMain;
import com.hc.res.ProductCategoryItem;
import com.hc.res.ProductSpecsItem;
import com.hc.service.ProductCategoryService;
import com.hc.service.ProductSpecsService;
import lombok.extern.slf4j.Slf4j;


import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhitb
 */
@Slf4j
@RestController
@RequestMapping("/productSpecs")
public class ProductSpecsController {

    @Resource
    private ProductSpecsService productSpecsService;

    @PostMapping
    public Result<Null> create(@RequestBody ProductSpecs productSpecs) {
        boolean res = productSpecsService.save(productSpecs);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result<Null> updateById(@RequestBody ProductSpecs productSpecs) {
        boolean res = productSpecsService.updateById(productSpecs);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result<Null> removeById(@PathVariable Serializable id) {
        boolean res = productSpecsService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result<ProductSpecs> getById(@PathVariable Serializable id) {
        ProductSpecs res = productSpecsService.getById(id);
        return ResultUtil.<ProductSpecs>success("查询成功").setData(res);
    }

    /**
     * 查询首页大图
     * @return
     */
    @GetMapping("/v1/index/ads")
    public Result<List<ProductSpecsItem>> indexAds() {
        ProductSpecsCondition condition = ProductSpecsCondition.builder()
                .pageSize(GlobalConstant.PageSize.INDEX_SECOND_CATEGORY_SIZE)
                // 上架
                .status(GlobalConstant.productStates.UP)
                .build();
        List<ProductSpecsItem> res = productSpecsService.queryByCondition(condition).getRecords();
        return ResultUtil.<List<ProductSpecsItem>>success("查询成功").setData(res);
    }

    /**
     * 查询推荐和促销
     * @return
     */
    @GetMapping("/v1/index/rp")
    public Result<List<ProductSpecsItem>> indexRecommendPromotion() {
        ProductSpecsCondition condition = ProductSpecsCondition.builder()
                .current(1L)
                .pageSize(GlobalConstant.PageSize.INDEX_SECOND_CATEGORY_SIZE)
                // 推荐
                .status(GlobalConstant.productStates.PROMOTION)
                .build();
        // 查询推荐
        PageBean<ProductSpecsItem> productRecommendPageBean = productSpecsService.queryByCondition(condition);
        // 查询促销
        condition.setStatus(GlobalConstant.productStates.RECOMMEND);
        PageBean<ProductSpecsItem> ProductPromotionPageBean = productSpecsService.queryByCondition(condition);

        List<ProductSpecsItem> res = new ArrayList<>();
        res.addAll(productRecommendPageBean.getRecords());
        res.addAll(ProductPromotionPageBean.getRecords());
        return ResultUtil.<List<ProductSpecsItem>>success("查询成功").setData(res);
    }

    @Resource
    private ProductCategoryService productCategoryService;

    /**
     * 查询当前一级类别 下的二级类别 的具体商品
     * @return
     */
    @GetMapping("/index/main")
    public Result<List<IndexMain>> getByCondition(){
        List<IndexMain> indexMainList = new ArrayList<>();

        // 不传递pid表示查询的是顶级父类
        ProductCategoryCondition condition = ProductCategoryCondition.builder()
                .columns("id,name")
                .pageSize(GlobalConstant.PageSize.FIRST_CATEGORY_SIZE)
                // 推荐
                .status(GlobalConstant.productStates.UP)
                .build();
        // 顶级类别
        List<ProductCategoryItem> productCategoryItemList = productCategoryService.queryByCondition(condition).getRecords();
        for (ProductCategoryItem productCategoryItem : productCategoryItemList) {
            IndexMain indexMain = new IndexMain();
            // 设置父类别
            indexMain.setProductCategoryItem(productCategoryItem);

            // 获取二级类别
            condition.setPid(productCategoryItem.getId());
            condition.setPageSize(GlobalConstant.PageSize.INDEX_SECOND_CATEGORY_SIZE);
            List<ProductCategoryItem> productCategoryItemList2 = productCategoryService.queryByCondition(condition).getRecords();

            // 设置子类别
            indexMain.setProductCategoryItemList(productCategoryItemList2);

            // 设置商品
            List<Integer> categoryIds= productCategoryItemList2.
                    stream().map(item -> item.getId()).collect(Collectors.toList());

            if(categoryIds != null && categoryIds.size()>0){
                ProductSpecsCondition condition3 = ProductSpecsCondition.builder()
                        .countryIds(categoryIds)
                        .pageSize(GlobalConstant.PageSize.INDEX_GOODS_SIZE)
                        .build();
                List<ProductSpecsItem> goodsItemList = productSpecsService.queryByCondition(condition3).getRecords();
                //设置商品
                indexMain.setProductSpecsItemList(goodsItemList);
            }

            indexMainList.add(indexMain);
        }
        return ResultUtil.<List<IndexMain>>success("查询成功").setData(indexMainList);

    }
}